package com.ruoyi.web.controller.coupons;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.coupons.domain.Coupons;
import com.ruoyi.coupons.service.ICouponsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 *
 * @author ruoyi
 * @date 2021-12-03
 */
@RestController
@RequestMapping("/coupons/coupons")
public class CouponsController extends BaseController
{
    @Autowired
    private ICouponsService couponsService;

    /**
     * 查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('coupons:coupons:list')")
    @GetMapping("/list")
    public TableDataInfo list(Coupons coupons)
    {
        startPage();
        List<Coupons> list = couponsService.selectCouponsList(coupons);
        return getDataTable(list);
    }
    /**
     * 查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('coupons:coupons:list')")
    @GetMapping("/getCanGiftCoupons")
    public TableDataInfo getCanGiftCoupons()
    {
        Coupons coupons = new Coupons();
        coupons.setState(DicConstants.SYS_Y.getCode());
        coupons.setBelongType(DicConstants.BELONG_TYPE_Plateform.getCode());
        List<Coupons> list = couponsService.selectCouponsList(coupons);
        List<Coupons> res = new ArrayList<>();
        long time = new Date().getTime();
        for (int i = 0; i < list.size(); i++){
            Coupons e = list.get(i);
     /*       //优惠券未在可使用区间
            if(e.getUseStartTime().getTime() < time || e.getUseEndTime().getTime() > time){
                continue;
            }
            //如果是限量券
            if(DicConstants.SYS_Y.getCode().equals(e.getLimitedState())){
                //判定一下是否有剩余数量
                if(e.getLastTotal() <= 0L)
                    continue;
            }*/
            res.add(e);
        }
        return getDataTable(res);
    }

    /**
     * 导出优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('coupons:coupons:export')")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Coupons coupons)
    {
        List<Coupons> list = couponsService.selectCouponsList(coupons);
        ExcelUtil<Coupons> util = new ExcelUtil<Coupons>(Coupons.class);
        return util.exportExcel(list, "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('coupons:coupons:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(couponsService.selectCouponsById(id));
    }

    /**
     * 新增优惠券
     */
    @PreAuthorize("@ss.hasPermi('coupons:coupons:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Coupons coupons)
    {
        if(DicConstants.SYS_N.getCode().equals(coupons.getFixedTimeState())){
            String s = DateUtils.dateTimeHMS(coupons.getUseEndTime());
            String[] s1 = s.split(" ");
            coupons.setUseEndTime(DateUtils.parseDate(s1[0]+" 23:59:59"));
        }
        coupons.setCreateType(DicConstants.BUSINESS_Manager.getCode());
        coupons.setLastTotal(coupons.getTotal());
        return toAjax(couponsService.insertCoupons(coupons));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('coupons:coupons:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Coupons coupons)
    {
        coupons.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(couponsService.updateCoupons(coupons));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('coupons:coupons:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PostMapping("/shelve/{id}/{state}")
    public AjaxResult shelve(@PathVariable Long id,@PathVariable String state)
    {
        return toAjax(couponsService.shelve(id,state));
    }

    /**
     * 删除优惠券
     */
    @PreAuthorize("@ss.hasPermi('coupons:coupons:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(couponsService.logicDeleteCoupons(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }
}
